Globalization in .NET
MSDN .NET Documentation > .NET > Globalization
Overview
Globalization (often abbreviated as G11n) is the process of designing and developing a software application so that it can be easily adapted to various languages and regions without engineering changes to its core code.
In .NET, globalization support is built into the framework and provides classes and methods for handling differences in:
- Culture-specific formatting of dates, numbers, and currency.
- String comparison and manipulation.
- Time zones and daylight saving time.
- Region-specific behaviors.
Effective globalization is crucial for creating applications that can reach a global audience and provide a seamless user experience across different locales.
Culture Information
The System.Globalization.CultureInfo
class is central to globalization in .NET. It represents a specific set of cultural information, such as language, region, and the use of conventions like date formats, currency symbols, and sorting orders.
You can obtain CultureInfo
objects for:
- The current culture of the operating system.
- A specific culture by its name (e.g., "en-US", "fr-FR", "es-ES").
- Invariant culture (used for culture-agnostic operations).
Example of getting the current culture:
using System.Globalization;
public class CultureExample
{
public static void DisplayCurrentCulture()
{
CultureInfo currentCulture = CultureInfo.CurrentCulture;
Console.WriteLine($"Current culture: {currentCulture.DisplayName}");
Console.WriteLine($"English name: {currentCulture.EnglishName}");
}
}
String Comparison
Comparing strings correctly is vital for globalization, as different cultures have different sorting rules. The string.Compare()
method and the CultureInfo.CompareInfo
class allow for culture-sensitive string comparisons.
Using string.Compare()
with a CultureInfo
:
using System.Globalization;
public class StringCompareExample
{
public static void CompareStrings()
{
string s1 = "Resume";
string s2 = "résumé";
CultureInfo frenchCulture = new CultureInfo("fr-FR");
int comparisonResult = string.Compare(s1, s2, frenchCulture, CompareOptions.None);
if (comparisonResult < 0)
{
Console.WriteLine($"'{s1}' comes before '{s2}' in French.");
}
else if (comparisonResult > 0)
{
Console.WriteLine($"'{s1}' comes after '{s2}' in French.");
}
else
{
Console.WriteLine($"'{s1}' and '{s2}' are equivalent in French.");
}
}
}
Formatting and Parsing
.NET provides powerful classes for formatting and parsing data according to culture-specific conventions. This includes:
- Number Formatting: Use
ToString()
on numeric types with aCultureInfo
, or useNumberFormatInfo
. - Date and Time Formatting: Use
ToString()
onDateTime
objects with aCultureInfo
, or useDateTimeFormatInfo
. - Currency Formatting: Use currency symbols and formats appropriate for the culture.
Example of formatting a date:
using System;
using System.Globalization;
public class DateTimeFormatExample
{
public static void FormatDate()
{
DateTime now = DateTime.Now;
CultureInfo usCulture = new CultureInfo("en-US");
CultureInfo deCulture = new CultureInfo("de-DE");
Console.WriteLine($"US format: {now.ToString("d", usCulture)}"); // Example: 10/26/2023
Console.WriteLine($"German format: {now.ToString("d", deCulture)}"); // Example: 26.10.2023
}
}
Parsing strings into numbers or dates also requires specifying the correct culture.
Time Zones
Handling time zones correctly is essential, especially for applications that operate across different geographical locations or deal with historical time data. The TimeZoneInfo
class provides comprehensive support for time zone data.
Key features include:
- Getting a list of available time zones.
- Converting times between time zones.
- Handling daylight saving time adjustments.
Example of converting time zones:
using System;
public class TimeZoneExample
{
public static void ConvertTime()
{
DateTime utcTime = DateTime.UtcNow;
TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime easternTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, easternZone);
Console.WriteLine($"UTC time: {utcTime}");
Console.WriteLine($"Eastern time: {easternTime}");
}
}
Regions
The RegionInfo
class allows you to retrieve information about a specific geographical region, such as its currency symbol, currency code, and sovereign state.
Example of region information:
using System.Globalization;
public class RegionExample
{
public static void GetRegionInfo()
{
RegionInfo usRegion = new RegionInfo("en-US");
Console.WriteLine($"US Currency Symbol: {usRegion.CurrencySymbol}"); // $
RegionInfo ukRegion = new RegionInfo("en-GB");
Console.WriteLine($"UK Currency Symbol: {ukRegion.CurrencySymbol}"); // £
}
}
Best Practices for Globalization
- Design for Localization from the Start: Don't try to add globalization to an application as an afterthought.
- Use Neutral Cultures for Resources: Store resource strings in satellite assemblies with neutral cultures (e.g.,
en
) and fall back to the invariant culture if necessary. - Avoid Hardcoding Text: All user-facing strings should be externalized into resource files.
- Use Culture-Sensitive Formatting: Always use
CultureInfo
when formatting dates, numbers, and currency. - Perform Culture-Sensitive String Comparisons: Use
string.Compare()
with appropriate cultures, especially for user input or data matching. - Handle Time Zones Carefully: Store dates in UTC and convert them to the user's local time zone for display.
- Test with Different Locales: Thoroughly test your application on systems with different regional settings.